package kr.co.redbean.frame
{
	import com.greensock.TimelineLite;
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.filters.BlurFilter;
	import flash.system.ApplicationDomain;
	import flash.text.AntiAliasType;
	import flash.text.Font;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.getDefinitionByName;
	
	import kr.co.redbean.store.AssetsAD;
	import kr.co.redbean.store.IObjectInfo;
	import kr.co.redbean.store.ObjectStore;
	
	import mx.effects.Tween;
	import mx.events.FlexEvent;
	
	import org.flexunit.internals.runners.statements.InvokeMethod;
	
	public class MemberFrame extends MainFrame implements IFrame
	{
		private var memberCanvas:Sprite;
		private var _tween:TimelineLite;
		private var _currentFocus:String = null;
		
		[Embed(source="/images/progress_circle.swf")]
		private var preloaderC:Class;
		private var preloader:MovieClip;
		private var preloaderContainer:Sprite;
		
		protected var _curObj:DisplayObject;
		
		public function MemberFrame()
		{
			super();
		
			memberCanvas = new Sprite;
			preloader = MovieClip(new preloaderC);
			preloaderContainer = new Sprite;
			_tween = new TimelineLite({paused:true});
			
			preloader.mouseEnabled = false;
			preloader.filters = [new BlurFilter(2,2)];
			
			preloaderContainer.mouseEnabled = false;
			preloaderContainer.alpha = 0;
			preloaderContainer.x = 0;
			preloaderContainer.y = 0;
			
			memberCanvas.mouseEnabled = false;
			
			addChild(memberCanvas);
			addChild(preloaderContainer);
			preloaderContainer.addChild(preloader);
			
			addEventListener(Event.ADDED_TO_STAGE, setPlacement);
		}

		public function doSetupMotion(objectInfo:IObjectInfo):void
		{
			memberCanvas.alpha = 1;
			preloaderContainer.scaleX = 0.7;
			preloaderContainer.scaleY = 0.7;
			preloaderContainer.x = objectInfo.properties.width/2 - preloaderContainer.width/2;
			preloaderContainer.y = objectInfo.properties.height/2 - preloaderContainer.height/2;
			
			_tween.append(new TweenLite(memberCanvas, .3, { height: objectInfo.properties.height }));
			_tween.append(new TweenLite(memberCanvas, .3, { width: objectInfo.properties.width }));
			_tween.play();
		}
		
		public function doProgressMotion(bytesLoaded:uint, bytesTotal:uint):void
		{
			_tween.append(new TweenLite(preloaderContainer, .3, { alpha: 1.0 }));
			_tween.play();
		}
		
		public function doCompleteMotion(displayObject:DisplayObject):void
		{
			_tween.append(new TweenLite(preloaderContainer, .3, { alpha: 0 }));
			_tween.append(new TweenLite(displayObject, .5, { alpha: 1.0 }));
			_tween.play();
		}
		
		private function onClose($e:Event):void {
			_tween.append(new TweenLite(memberCanvas, .3, { width: 1 }));
			_tween.append(new TweenLite(memberCanvas, .3, { height: 1 }));
			_tween.append(new TweenLite(memberCanvas, .1, { alpha: 0 }));
			_tween.play();
			
			// 로딩됐었던 외부swf를 제거함
			removeChild(_curObj);
			_currentFocus = null;
		}
		
		public function setPlacement($e:Event=null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, setPlacement);
			
			setCanvasPlacement();
			setPosition();
		}
		
		private function setPosition():void {
			x = int(posF.left);
			y = int(posF.top);
		}
		private function setCanvasPlacement():void {
			memberCanvas.graphics.clear();
			memberCanvas.graphics.beginFill(0x2b1a1b);
			memberCanvas.graphics.drawRect(0, 0, 1, 1);
			memberCanvas.graphics.endFill();
			memberCanvas.alpha = 0;
		}
		public function addObject(obj:DisplayObject):void {
			obj.alpha = 0;
			obj.addEventListener(Event.CLOSE, onClose);
			_curObj = obj;
			addChild(obj);
			setPosition();
		}

		public function get currentFocus():String
		{
			return _currentFocus;
		}

		public function set currentFocus(value:String):void
		{
			_currentFocus = value;
		}


	}
}